let { ipcMain, BrowserWindow } = require('electron')
let path = require('path')

let newWin = null

// 打开新窗口
ipcMain.on('openNewWin', (e, data) => {

  let mainWinId = BrowserWindow.getFocusedWindow().id

  newWin = new BrowserWindow({
    width: 600,
    height: 600,
    // 标题
    title: '新窗口'
  })

  newWin.webContents.openDevTools()

  newWin.loadURL(path.resolve(__dirname, '../new.html'))

  newWin.webContents.on('did-finish-load', () => {
    console.log('子窗口web加载完毕 主窗口带来的数据', data)
    newWin.webContents.send('openNewsFirst', {
      ...data,
      mainWinId
    })
  })

  newWin.on('close', () => {
    newWin = null
  })
})

// 主窗口向子窗口发送消息
ipcMain.on('mainSendNewMsg', (e, data) => {
  newWin.webContents.send('applyMesg', data)
})